home *** CD-ROM | disk | FTP | other *** search
Wrap
/* Miscellany.c */ /* C14 Calculator */ #include <Types.h> #include <Quickdraw.h> #include <Controls.h> #include <Dialogs.h> /* for ok */ #include <Events.h> /*#include <Lists.h>*/ #include <Menus.h> #include <TextEdit.h> #include "Globals.h" #include "ResourceDefs.h" #include "Miscellany.h" #include <Errors.h> #include <Packages.h> #include <Resources.h> #include <Script.h> /* for GetMBarHeight */ #include <Sound.h> #pragma segment Miscellany void MiscellanySeg() {} /* global vars: */ Boolean errorFlag; #define createTop 75 #define createLeft 100 #define topLeft(r) (((Point *) &(r))[0]) #define botRight(r) (((Point *) &(r))[1]) /*----------*/ void Acknowledge (short alertID) { short itemHit; GrafPtr savePort; GetPort (&savePort); InitCursor (); itemHit = StopAlert (alertID, NULL); SetPort (savePort); } /*Acknowledge*/ /*----------*/ Boolean Confirm (short alertID) { Boolean result; GrafPtr savePort; GetPort (&savePort); InitCursor (); result = (CautionAlert (alertID, NULL) == ok); SetPort (savePort); return (result); } /*Confirm*/ /*----------*/ Boolean GetErrorMessage (OSErr resultCode, Str255 message); Boolean GetErrorMessage (OSErr resultCode, Str255 message) { StringHandle msgHndl; msgHndl = (StringHandle) GetResource ('ErMs', -resultCode); if (msgHndl != NULL) { BlockMove (&(**msgHndl), message, 256); return (true); } else { message [0] = 0; return (false); } } /*GetErrorMessage*/ /*----------*/ Boolean CheckOS (OSErr resultCode) { Str255 message; Str255 errNum; if (resultCode == noErr) { return (true); } else { if (GetErrorMessage (resultCode, message)) { ParamText (message, "\p", "\p", "\p"); } else { /*generic message*/ if (!GetErrorMessage (0, message)) { BlockMove ("\pOS Error ", message, 10); } NumToString (resultCode, errNum); ParamText (message, errNum, "\p", "\p"); } Acknowledge (IOErrorID); errorFlag = true; return (false); } } /*CheckOS*/ #ifndef THINK_C /*MPW C*/ static StringPtr failNilRsrcText = {"\pSorry, a resource needed by this application cannot be found. Click the mouse or press any key to quit."}; #else /*THINK C*/ static StringPtr failNilRsrcText = {"\pSorry, a resource needed by this application cannot be found. THINK C requires that your resource file (AppMaker document) be in the same folder and have the same name as the project file with \".rsrc\" appended. Click the mouse or press any key to quit."}; #endif /*----------*/ /* If resource = nil, this procedure puts up a window reporting that a resource */ /* could not be found, then quits the program. */ /*----------*/ void FailNilResource (Handle resource) { Rect windowBounds; /* global coordinates */ WindowPtr window; Rect textBounds; /* local coordinates */ if (resource == nil) { SetRect (&windowBounds, 90 /*left*/, 70 /*top*/, 420 /*right*/, 200 /*bottom*/); window = NewWindow (nil /*wStorage*/, &windowBounds, "\p" /*title*/, true /*visFlag*/, dBoxProc /*wDefProcID*/, (WindowPtr)-1 /*behind*/, false /*goAwayFlag*/, 0L /*refCon*/); if (window != nil) { SetPort (window); InitCursor (); SetRect (&textBounds, 20 /*left*/, 20 /*top*/, 320 /*right*/, 120 /*bottom*/); TextBox (&(failNilRsrcText [1]), failNilRsrcText [0], &textBounds, teJustLeft); while (!GetNextEvent (mDownMask + keyDownMask, &curEvent)) ; /*wait for the next mouseDown or keyDown event*/ DisposeWindow (window); } else { /*window == nil, NewWindow must have failed*/ SysBeep (1); } ExitToShell (); } /* otherwise, resource != nil, do nothing */ } /*FailNilResource*/ /*----------*/ Boolean FileExists (Str255 fName, short vRefNum) { FInfo fileInfo; return (GetFInfo (fName, vRefNum, &fileInfo) == noErr); } /*FileExists*/ /*----------*/ Boolean CreateFile (SFReply *sfInfo, Str255 prompt, Str255 suggestion, OSType creator, OSType fileType) { Point dlgOrigin; Boolean okay; SetPt (&dlgOrigin, createLeft, createTop); SFPutFile (dlgOrigin, prompt, suggestion, NULL, sfInfo); okay = sfInfo->good; if (okay) { if (FileExists (sfInfo->fName, sfInfo->vRefNum)) { okay = CheckOS (FSDelete (sfInfo->fName, sfInfo->vRefNum)); } } if (okay) { okay = CheckOS (Create (sfInfo->fName, sfInfo->vRefNum, creator, fileType)); } return (okay); } /*CreateFile*/ /*----------*/ void ScaleWindow (WindowPtr window, Boolean scaleSize) { Rect newBounds; Rect origScreen; Rect curScreen; short height; short width; newBounds = window->portRect; LocalToGlobal (&topLeft (newBounds)); LocalToGlobal (&botRight (newBounds)); SetRect (&origScreen, 0, 20, 512, 342); curScreen = qd.screenBits.bounds; curScreen.top = GetMBarHeight (); height = newBounds.bottom - newBounds.top; width = newBounds.right - newBounds.left; MapRect (&newBounds, &origScreen, &curScreen); if (scaleSize) { SizeWindow (window, newBounds.right - newBounds.left, newBounds.bottom - newBounds.top, true); } else { newBounds.top = newBounds.bottom - height; newBounds.left = newBounds.right - width; } MoveWindow (window, newBounds.left, newBounds.top, false); } /*ScaleWindow*/ /*----------*/ void DrawClippedGrow (short x, short y) { RgnHandle saveClip; Rect growRect; Rect portRect; saveClip = NewRgn (); GetClip (saveClip); portRect = qd.thePort->portRect; if (x >= 0) { x = portRect.left + x; } else { x = portRect.right + x; } if (y >= 0) { y = portRect.top + y; } else { y = portRect.bottom + y; } SetRect (&growRect, x, y, portRect.right, portRect.bottom); ClipRect (&growRect); DrawGrowIcon (qd.thePort); SetClip (saveClip); DisposeRgn (saveClip); } /*DrawClippedGrow*/ /*----------*/ void DoRadioMenu (MenuHandle menu, short firstItem, short lastItem, short itemNr) { short i; for (i = firstItem; i <= lastItem; i++) { CheckItem (menu, i, (i == itemNr)); } } /*DoRadioMenu*/ /*----------*/ void PlaySound (MenuHandle soundsMenu, short itemNr) { Str255 soundName; /* cw5 */ //SndListHandle sound; OSErr errCode; GetItem (soundsMenu, itemNr, soundName); /* cw5 */ //sound = GetNamedResource ('snd ', soundName); //if (sound != NULL) { //errCode = SndPlay (NULL, sound, false); //} } /*PlaySound*/ /*----------*/ /* The following routines implement a linked list of Items.*/ /* These routines expect that the first field of every item*/ /* is a pointer (Handle) to the next item. They make no*/ /* assumptions about the remainder of each item.*/ /* The implementation happens to be a singly linked list.*/ /* It is linked circularly with the last item pointing*/ /* to the first item. The listHead points to the*/ /* last item. This implementation provides fast*/ /* access to both the head and tail of the list*/ /* without the extra space of a doubly linked list.*/ /*----------*/ void LinkLast (Handle *listHead, Handle newItem) { LinkedItem item; LinkedItem last; item = (LinkedItem) newItem; if (*listHead == NULL) { (**item).next = item; } else { last = (LinkedItem) *listHead; (**item).next = (**last).next; (**last).next = item; } *listHead = (Handle) item; } /*LinkLast*/ /*----------*/ Handle GetNth (Handle listHead, short num) { LinkedItem item; short I; item = NULL; if (listHead != NULL) { item = (LinkedItem) listHead; for (I = 1; I <= num; I++) { item = (**item).next; } /*for*/ } return ((Handle) item); } /*GetNth*/ /*----------*/ Handle UnlinkNth (Handle *listHead, short num) { LinkedItem item; LinkedItem prev; short I; item = NULL; if (*listHead != NULL) { prev = (LinkedItem) *listHead; for (I = 1; I <= num - 1; I++) { prev = (**prev).next; } /*for*/ item = (**prev).next; if (item == prev) { *listHead = NULL; } else { (**prev).next = (**item).next; if (*listHead == (Handle) item) { *listHead = (Handle) prev; } } } return ((Handle) item); } /*UnlinkNth*/ /* Miscellany */